<?php


class Main_Model_User extends WOW_Model_Abstract
{
    protected $_key = 'WrathOftheLichKing';
    
    public function getResourceId()
    {
        return 'User';
    }

    public function getUserById($id)
    {
        $id = (int) $id;
        return $this->getResource('User')->getUserById($id);
    }

    public function getUserByEmail($name)
    {
        return $this->getResource('User')
                    ->getUserByEmail($name);
    }
    
    public function getUserByEmailPassword($email, $pwd)
    {
        return $this->getResource('User')
                    ->getUserByEmailPassword($email, $pwd);
    }

    public function getUserByVerificationCode($vcode){
        return $this->getResource('User')
                    ->getUserByVerificationCode($vcode);
    }
    
    public function createUser($post)
    {
        $data = array();
        if( isset( $post['UserEmail'] ) )
            $data['UserEmail'] = trim($post['UserEmail']);

        if( isset( $post['FirstName']) )
            $data['FirstName'] = trim($post['FirstName']);

        if( isset( $post['LastName']))
            $data['LastName'] = trim($post['LastName']);

         if( isset( $post['Gender']) )
            $data['Gender'] = trim($post['Gender']);

          if( isset( $post['password']) )
            $data['Password'] = trim($post['password']);

          $data['VerificationCode'] = $this->_generateVerificationCode();

          if($this->_saveUser($data) )
              return true;
          else
              return false;

    }

    public function saveUser($post)
    {
        $data = array();
        $user = $this->getResource('User')->getUserById($post['uid']);
        $data['Password']   = $user->Password;
        $data['uid']        = $post['uid'];
        $data['VerificationCode'] = $user->VerificationCode;
        $data['registered_at'] = $user->registered_at;

        if( isset($post['UserEmail']) )
            $data['UserEmail'] = trim($post['UserEmail']);

        if( isset($post['Salutation']) )
            $data['Salutation'] = trim($post['Salutation']);
        
        if( isset( $post['FirstName']) )
            $data['FirstName'] = trim($post['FirstName']);

        if( isset( $post['MiddleName']) )
            $data['MiddleName'] = trim($post['MiddleName']);

        if( isset( $post['LastName']))
            $data['LastName'] = trim($post['LastName']);

        if( isset( $post['PreviousFirstName']))
            $data['PreviousFirstName'] = trim($post['PreviousFirstName']);

        if( isset($post['PreviousMiddleName'])) 
            $data['PreviousMiddleName'] = trim($post['PreviousMiddleName']);

        if( isset( $post['PreviousLastName'] ) )
            $data['PreviousLastName'] = trim($post['PreviousLastName']);

        if( isset( $post['PreviousMiddleName']))
            $data['PreviousMiddleName'] = trim($post['PreviousMiddleName']);

        if( isset( $post['DateOfBirth'])) 
            $data['DateOfBirth'] = trim($post['DateOfBirth']);

        if( isset( $post['PlaceOfBirth']))
            $data['PlaceOfBirth'] = trim($post['PlaceOfBirth']);

        if( isset( $post['Gender']) )
            $data['Gender'] = trim($post['Gender']);

        if( isset( $post['PhysicalIdentityMark']) )
            $data['PhysicalIdentityMark'] = trim($post['PhysicalIdentityMark']);

        if( isset( $post['PermanentAccountNumber']))
            $data['PermanentAccountNumber'] = trim($post['PermanentAccountNumber']);

        if( isset( $post['Qualification']))
            $data['Qualification'] = trim($post['Qualification']);

        if( isset( $post['Occupation']))
            $data['Occupation'] = trim($post['Occupation']);

        if( isset( $post['Qualification']))
            $data['Qualification'] = trim($post['Qualification']);

        if( isset( $post['RecentPhoto']))
            $data['RecentPhoto'] = trim($post['RecentPhoto']);

        if( isset( $post['PhysicallyChallenged']))
            $data['PhysicallyChallenged'] = trim($post['PhysicallyChallenged']);

        if( isset( $post['PhysicallyChallengedDescription']))
            $data['PhysicallyChallengedDescription'] = trim($post['PhysicallyChallengedDescription']);

        if( isset( $post['MobileNumber']))
            $data['MobileNumber'] = trim($post['MobileNumber']);

        if( isset( $post['BestPhoneToReach']))
            $data['BestPhoneToReach'] = trim($post['BestPhoneToReach']);

        if( isset( $post['AlternateEmail']))
            $data['AlternateEmail'] = trim($post['AlternateEmail']);

        if( isset( $post['TermsOfEmployment']))
            $data['TermsOfEmployment'] = trim($post['TermsOfEmployment']);

        if( isset( $post['FuneralRequests']) )
            $data['FuneralRequests'] = trim($post['FuneralRequests']);

        if( isset( $post['FamilyDisclosure']) )
            $data['FamilyDisclosure'] = trim($post['FamilyDisclosure']);

        if($this->_saveUser($data) )
              return true;
          else
              return false;

    }

    public function changePassword($data){
        if($this->_savePassword($data))
              return true;
          else
              return false;
    }
    
    protected function _savePassword($data, $defaults=array()   )
    {
        foreach ($defaults as $col => $value)
        {
            $data[$col] = $value;
        }
        if($data['uid'] !='')
        {
            $data['updated_at']     = date('Y-m-d H:i:s');
        }
        else
        {
            $data['created_at'] = date('Y-m-d H:i:s');
            $data['updated_at'] = date('Y-m-d H:i:s');
        }

        $user = ($data['uid'] !='') ? $this->getResource('User')->getUserById($data['uid']) : null;
        
        return $this->getResource('user')->saveRow($data, $user);
    }
    
    public function verifyUser($details){
        
        $data['uid'] = $details['uid'];
        $data['DisableFlag'] = 0;
        $data['IsUserVerified'] = 1;
        $data['IsEmailVerified'] = 1;
        
        return $this->_saveUser($data);
    }
    
    protected function _saveUser(   $data, $defaults=array()   )
    {
        foreach ($defaults as $col => $value)
        {
            $data[$col] = $value;
        }
        if($data['uid'] !='')
        {
            $data['updated_at']     = date('Y-m-d H:i:s');
        }
        else
        {
            $data['created_at'] = date('Y-m-d H:i:s');
            $data['updated_at'] = date('Y-m-d H:i:s');
        }

        $user = ($data['uid'] !='') ? $this->getResource('User')->getUserById($data['uid']) : null;
        
        return $this->getResource('user')->saveRow($data, $user);
    }

    protected function _generateVerificationCode()
    {
        return MD5( $this->_key . time() . mt_rand(5, 500) );
    }
    
}
?>
